package com.ruoyi.project.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.ruoyi.project.system.entity.SysMenu;
import com.ruoyi.project.system.entity.SysRoleMenu;
import com.ruoyi.project.system.mapper.SysMenuMapper;
import com.ruoyi.project.system.mapper.SysRoleMenuMapper;
import com.ruoyi.project.system.service.ISysMenuService;
import lombok.AllArgsConstructor;
import net.dreamlu.mica.mybatis.service.impl.MicaServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 菜单 业务层实现类
 *
 * @author dy
 */
@Service
@AllArgsConstructor
public class SysMenuServiceImpl extends MicaServiceImpl<SysMenuMapper, SysMenu> implements ISysMenuService {

    private final SysMenuMapper menuMapper;

    private final SysRoleMenuMapper roleMenuMapper;

    @Override
    public List<SysMenu> listUserMenuByRoles(List<Long> roleIds) {
        // sys_role_menu
        List<SysRoleMenu> roleMenuList = roleMenuMapper.selectList(
                new LambdaQueryWrapper<SysRoleMenu>()
                        .in(SysRoleMenu::getRoleId, roleIds));

        if (!CollectionUtils.isEmpty(roleMenuList)) {
            // sys_menu
            return menuMapper.selectList(new LambdaQueryWrapper<SysMenu>()
                    .in(SysMenu::getId, roleMenuList.stream().map(SysRoleMenu::getMenuId).collect(Collectors.toSet()))
                    .orderByAsc(SysMenu::getSeq));
        }

        return new ArrayList<>();
    }
}
